package com.dyna.demo.common;

import com.dyna.core.exception.CustomException;
import com.dyna.domain.resp.R;
import com.dyna.domain.resp.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Api("demo模块/demo/通用")
@RestController
@RequestMapping("/common")
public class CommonController {

    private final CommonService commonService;

    @Autowired
    public CommonController(CommonService commonService) {
        this.commonService = commonService;
    }

    @ApiOperation("for循环")
    @RequestMapping("/forLoop")
    public R<Boolean> forLoop() {
        return R.isSuccess(commonService.forLoop());
    }

    @ApiOperation("while循环")
    @RequestMapping("/whileLoop")
    public R<Boolean> whileLoop() {
        return R.isSuccess(commonService.whileLoop());
    }

    @ApiOperation("生成csv测试数据")
    @GetMapping("/createCsvDataFile")
    public R<Boolean> createCsvDataFile(
            @RequestBody Map<String, String> map
            ) {
        return R.isSuccess(commonService.createCsvDataFile(map));
    }

    @ApiOperation("测试接收JMeter Csv文件测试数据")
    @RequestMapping("/test-cvs")
    public R<Boolean> testCvs(@RequestParam("str1") String str1,
                              @RequestParam("str2") String str2
                              ) {
        log.info("str1:::{}", str1);
        log.info("str2:::{}", str2);
        return R.isSuccess(Boolean.TRUE);
    }

    @ApiOperation("参数回调")
    @RequestMapping("/callback")
    public R<Boolean> callback() {
        return R.isSuccess(commonService.callback());
    }

    @ApiOperation(value = "测试网管servlet携带请求头", notes = "经过验证，网关即使不往ServerHttpRequest中set请求头信息也能获取到")
    @RequestMapping("/testGatewayServlet")
    public R<Boolean> testGatewayServlet(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        System.out.println(userId);
        return R.isSuccess(Boolean.TRUE);
    }

    @ApiOperation("测试接收JMeter Csv文件测试数据")
    @RequestMapping("/sleep")
    public R<Boolean> sleep() {
        log.info("请求到了...");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return R.isSuccess(Boolean.TRUE);
    }

    @ApiOperation("测试抛异常")
    @RequestMapping("/testException")
    public R<Boolean> testException() {
        try {
            int i = 1/0;
        }catch (Exception e) {
            log.info("异常信息:::"+e.getMessage());
            throw new CustomException(ResultCode.MEDIA_TYPE_NOT_SUPPORTED,"测试异常111");
        }
        return R.data(Boolean.TRUE);
    }

}


























